// pages/user-list/user-list.js
import {
  HTTP
} from '../../utils/http'
let http = new HTTP();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fansList: [],
    fansPageSize: 10,
    fansPageNum: 1,
    fansTotal: 0,
    focusList: [],
    focusPageSize: 10,
    focusPageNum: 1,
    focusTotal: 0,
    accountId: null,
    active: 0
  },

  onTabsChange(e) {
    this.setData({
      active: e.detail.index,
      focusList: [],
      focusPageSize: 10,
      focusPageNum: 1,
      focusTotal: 0,
      fansList: [],
      fansPageSize: 10,
      fansPageNum: 1,
      fansTotal: 0
    })
    let index = e.detail.index
    if (index === 0) {
      //粉丝切换
      this.getFansList(this.data.accountId)
    } else if (index === 1) {
      //关注切换
      this.getFocusList(this.data.accountId)
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const accountId = options.accountId;
    this.setData({
      accountId: accountId
    })
    this.getFansList(accountId);
    this.getFocusList(accountId);
  },

  getFansList(accountId) {
    http.request({
      url: '/ai/draw/account/get/page/focus',
      method: 'post',
      data: {
        "pageSize": this.data.fansPageSize,
        "pageNum": this.data.fansPageNum,
        "focusType": 2,
        accountId
      },
      success: (res) => {
        if (res.code === "0") {
          let list = res.data.list
          var arr1 = this.data.fansList;
          var arr2 = list;
          arr1 = arr1.concat(arr2);
          this.setData({
            fansList: arr1,
            fansTotal: res.data.total
          })
        }
      }
    })
  },

  refreshFansList() {
    this.setData({
      fansList: [],
      fansPageSize: 10,
      fansPageNum: 1,
      fansTotal: 0
    })
    this.getFansList(this.data.accountId);
  },

  getFocusList(accountId) {
    http.request({
      url: '/ai/draw/account/get/page/focus',
      method: 'post',
      data: {
        "pageSize": this.data.focusPageSize,
        "pageNum": this.data.focusPageNum,
        "focusType": 1,
        accountId
      },
      success: (res) => {
        if (res.code === "0") {
          let list = res.data.list
          var arr1 = this.data.focusList;
          var arr2 = list;
          arr1 = arr1.concat(arr2);
          this.setData({
            focusList: arr1,
            focusTotal: res.data.total
          })
        }
      }
    })
  },

  refreshFocusList() {
    this.setData({
      focusList: [],
      focusPageSize: 10,
      focusPageNum: 1,
      focusTotal: 0
    })
    this.getFocusList(this.data.accountId);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.log("asdas132");
    if (this.data.active === 0) {
      // 粉丝列表
      if (!(this.data.fansList && this.data.fansList.length > 0)) return
      let currentTotal = this.data.fansList.length;
      if (currentTotal <= this.data.fansTotal) {
        var current = this.data.fansPageNum * 1 + 1;
        this.setData({
          fansPageNum: current
        })
        this.getFansList(this.data.q);
      }
    } else if (this.data.active === 1) {
      // 关注列表
      if (!(this.data.focusList && this.data.focusList.length > 0)) return
      let currentTotal = this.data.focusList.length;
      if (currentTotal <= this.data.focusTotal) {
        var current = this.data.focusPageNum * 1 + 1;
        this.setData({
          focusPageNum: current
        })
        this.getFocusList(this.data.q);
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})